/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import handler.database;
import java.sql.*;

/**
 *
 * @author ifan
 */
public class InsertCategory extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet InsertCategory</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet InsertCategory at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/plain;charset=UTF-8");

        PrintWriter out = response.getWriter();
        Connection conn = null;

        String strQuery = "";
        String strQuery1 = "";
        String category_name = "";
        String user_name = "";

        Statement statement = null;
        Statement statement1 = null;
        ResultSet result = null;
        ResultSet result1 = null;
        int Result;
        try {
             database db = new database();
             conn = db.getConnection();

             boolean IsModerator = false;
             int owner_id = 0;

             user_name = request.getParameter("owner");
             strQuery1 = "SELECT * from user where username='"+ user_name +"'";
             System.out.println(strQuery1);
             statement1 = conn.createStatement();
             result1 = statement1.executeQuery(strQuery1);

             while(result1.next()){
                if(result1.getString(5).equalsIgnoreCase("moderator")){
                    IsModerator = true;
                    owner_id = Integer.parseInt(result1.getString(1));
                }
             }
            
             if(request.getParameter("category") != null && request.getParameter("category").length() > 0 && IsModerator == true){
                category_name = request.getParameter("category").toString();
                strQuery = "INSERT INTO category (name,owner_id ) "
                        + "VALUES ('"+ category_name + "'," + owner_id + ")";
                System.out.println(strQuery);
                statement = conn.createStatement();
                Result = statement.executeUpdate(strQuery);
                out.println("succes");
             } else {
                 out.println("fail");
             }
             
        } catch (SQLException ex) {
            out.println("fail");
            Logger.getLogger(InsertCategory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            out.println("fail");
            Logger.getLogger(InsertCategory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            out.println("fail");
            Logger.getLogger(InsertCategory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            out.println("fail");
            Logger.getLogger(InsertCategory.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
